{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-07-15T05:36:03.112422Z",
     "start_time": "2024-07-15T05:35:23.991775Z"
    }
   },
   "source": [
    "import  pandas as pd\n",
    "from sklearn.preprocessing import  LabelEncoder,StandardScaler\n",
    "import csv\n",
    "df=pd.read_csv('Asia towers.csv')\n",
    "\n",
    "#处理缺省值\n",
    "df.fillna('unknown',inplace=True)\n",
    "categorical_columns =[]\n",
    "with open('Asia towers.csv', 'r') as file:\n",
    "    csv_reader=csv.reader(file)\n",
    "    header=next(csv_reader)\n",
    "    categorical_columns.append(header)\n",
    "print (categorical_columns)"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\WPP-JKW\\AppData\\Local\\Temp\\ipykernel_29136\\1885926109.py:4: DtypeWarning: Columns (16) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  df=pd.read_csv('Asia towers.csv')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['', 'radio', 'MCC', 'MNC', 'TAC', 'CID', 'unit', 'LON', 'LAT', 'RANGE', 'SAM', 'changeable', 'created', 'updated', 'averageSignal', 'Country', 'Network', 'Continent']]\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-15T05:46:56.760118Z",
     "start_time": "2024-07-15T05:46:02.717854Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数据预处理：将类别型变量转换为数值型变量\n",
    "label_encoders={}\n",
    "categorical_columns=[ 'radio', 'MCC', 'MNC', 'TAC', 'CID', 'unit', 'LON', 'LAT', 'RANGE', 'SAM', 'changeable', 'created', 'updated', 'averageSignal', 'Country', 'Network', 'Continent']\n",
    "uniques={}\n",
    "\n",
    "mapping={}\n",
    "for col in categorical_columns:\n",
    "    uniques[col] = df[col].unique()\n",
    "\n",
    "for col in categorical_columns:\n",
    "    le=LabelEncoder()\n",
    "    df[col] = le.fit_transform(df[col])\n",
    "    label_encoders[col]=le\n",
    "    mapping[col] = dict(zip(le.transform(le.classes_),le.classes_))\n",
    "  #  for a,b in mapping[col].items():\n",
    "#print(f'{col}: {b} -> {a}')\n",
    "# 标准化数值变量"
   ],
   "id": "b765f5a155dcc4c2",
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "Encoders require their input argument must be uniformly strings or numbers. Got ['int', 'str']",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "File \u001B[1;32mD:\\Python Codes\\.conda\\Lib\\site-packages\\sklearn\\utils\\_encode.py:174\u001B[0m, in \u001B[0;36m_unique_python\u001B[1;34m(values, return_inverse, return_counts)\u001B[0m\n\u001B[0;32m    172\u001B[0m uniques_set, missing_values \u001B[38;5;241m=\u001B[39m _extract_missing(uniques_set)\n\u001B[1;32m--> 174\u001B[0m uniques \u001B[38;5;241m=\u001B[39m \u001B[38;5;28msorted\u001B[39m(uniques_set)\n\u001B[0;32m    175\u001B[0m uniques\u001B[38;5;241m.\u001B[39mextend(missing_values\u001B[38;5;241m.\u001B[39mto_list())\n",
      "\u001B[1;31mTypeError\u001B[0m: '<' not supported between instances of 'int' and 'str'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[8], line 12\u001B[0m\n\u001B[0;32m     10\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m col \u001B[38;5;129;01min\u001B[39;00m categorical_columns:\n\u001B[0;32m     11\u001B[0m     le\u001B[38;5;241m=\u001B[39mLabelEncoder()\n\u001B[1;32m---> 12\u001B[0m     df[col] \u001B[38;5;241m=\u001B[39m le\u001B[38;5;241m.\u001B[39mfit_transform(df[col])\n\u001B[0;32m     13\u001B[0m     label_encoders[col]\u001B[38;5;241m=\u001B[39mle\n\u001B[0;32m     14\u001B[0m     mapping[col] \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mdict\u001B[39m(\u001B[38;5;28mzip\u001B[39m(le\u001B[38;5;241m.\u001B[39mtransform(le\u001B[38;5;241m.\u001B[39mclasses_),le\u001B[38;5;241m.\u001B[39mclasses_))\n",
      "File \u001B[1;32mD:\\Python Codes\\.conda\\Lib\\site-packages\\sklearn\\preprocessing\\_label.py:115\u001B[0m, in \u001B[0;36mLabelEncoder.fit_transform\u001B[1;34m(self, y)\u001B[0m\n\u001B[0;32m    102\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Fit label encoder and return encoded labels.\u001B[39;00m\n\u001B[0;32m    103\u001B[0m \n\u001B[0;32m    104\u001B[0m \u001B[38;5;124;03mParameters\u001B[39;00m\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m    112\u001B[0m \u001B[38;5;124;03m    Encoded labels.\u001B[39;00m\n\u001B[0;32m    113\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m    114\u001B[0m y \u001B[38;5;241m=\u001B[39m column_or_1d(y, warn\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mTrue\u001B[39;00m)\n\u001B[1;32m--> 115\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mclasses_, y \u001B[38;5;241m=\u001B[39m _unique(y, return_inverse\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mTrue\u001B[39;00m)\n\u001B[0;32m    116\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m y\n",
      "File \u001B[1;32mD:\\Python Codes\\.conda\\Lib\\site-packages\\sklearn\\utils\\_encode.py:42\u001B[0m, in \u001B[0;36m_unique\u001B[1;34m(values, return_inverse, return_counts)\u001B[0m\n\u001B[0;32m     11\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Helper function to find unique values with support for python objects.\u001B[39;00m\n\u001B[0;32m     12\u001B[0m \n\u001B[0;32m     13\u001B[0m \u001B[38;5;124;03mUses pure python method for object dtype, and numpy method for\u001B[39;00m\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m     39\u001B[0m \u001B[38;5;124;03m    array. Only provided if `return_counts` is True.\u001B[39;00m\n\u001B[0;32m     40\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[0;32m     41\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m values\u001B[38;5;241m.\u001B[39mdtype \u001B[38;5;241m==\u001B[39m \u001B[38;5;28mobject\u001B[39m:\n\u001B[1;32m---> 42\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m _unique_python(\n\u001B[0;32m     43\u001B[0m         values, return_inverse\u001B[38;5;241m=\u001B[39mreturn_inverse, return_counts\u001B[38;5;241m=\u001B[39mreturn_counts\n\u001B[0;32m     44\u001B[0m     )\n\u001B[0;32m     45\u001B[0m \u001B[38;5;66;03m# numerical\u001B[39;00m\n\u001B[0;32m     46\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m _unique_np(\n\u001B[0;32m     47\u001B[0m     values, return_inverse\u001B[38;5;241m=\u001B[39mreturn_inverse, return_counts\u001B[38;5;241m=\u001B[39mreturn_counts\n\u001B[0;32m     48\u001B[0m )\n",
      "File \u001B[1;32mD:\\Python Codes\\.conda\\Lib\\site-packages\\sklearn\\utils\\_encode.py:179\u001B[0m, in \u001B[0;36m_unique_python\u001B[1;34m(values, return_inverse, return_counts)\u001B[0m\n\u001B[0;32m    177\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mTypeError\u001B[39;00m:\n\u001B[0;32m    178\u001B[0m     types \u001B[38;5;241m=\u001B[39m \u001B[38;5;28msorted\u001B[39m(t\u001B[38;5;241m.\u001B[39m\u001B[38;5;18m__qualname__\u001B[39m \u001B[38;5;28;01mfor\u001B[39;00m t \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mset\u001B[39m(\u001B[38;5;28mtype\u001B[39m(v) \u001B[38;5;28;01mfor\u001B[39;00m v \u001B[38;5;129;01min\u001B[39;00m values))\n\u001B[1;32m--> 179\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mTypeError\u001B[39;00m(\n\u001B[0;32m    180\u001B[0m         \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mEncoders require their input argument must be uniformly \u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m    181\u001B[0m         \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mstrings or numbers. Got \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mtypes\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m    182\u001B[0m     )\n\u001B[0;32m    183\u001B[0m ret \u001B[38;5;241m=\u001B[39m (uniques,)\n\u001B[0;32m    185\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m return_inverse:\n",
      "\u001B[1;31mTypeError\u001B[0m: Encoders require their input argument must be uniformly strings or numbers. Got ['int', 'str']"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "cb116f655ea306b2"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
